package org.example.Soution002;

import java.util.ArrayList;
import java.util.List;

public class BracketGenerate {
    public static void main(String[] args) {
        BracketGenerate obj = new BracketGenerate();
        System.out.println(obj.generateParenthesis(3));
    }

    List<String> result = new ArrayList<>();
    public List<String> generateParenthesis(int n) {
        StringBuilder sb = new StringBuilder();
        sb.append('(');
        int left=1;int right=0;
        getBracketCom(sb,left,right,n);
        return result;
    }

    public void getBracketCom(StringBuilder sb, int left, int right, int n) {
        //结束
        if(left==n&&left==right){
            result.add(sb.toString());
            return;
        }
        if(left<n){
            StringBuilder leftSb=new StringBuilder(sb);
            leftSb.append('(');
            getBracketCom(leftSb,left+1,right,n);
        }
        if(right<left){
            StringBuilder rightSb=new StringBuilder(sb);
            rightSb.append(')');
            getBracketCom(rightSb,left,right+1,n);
        }
    }
}
